package com.roc.steelend.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.returnformat.ReturnMsgForTable;
import com.roc.steelend.controller.vo.specialInvoice.SpecialInvoiceBySaleReception;
import com.roc.steelend.controller.vo.specialPay.SpecialBySaleReception;
import com.roc.steelend.pojo.Client;
import com.roc.steelend.service.ClientService;
import com.roc.steelend.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@RestController
@CrossOrigin
@Api(tags = "客户功能接口")
public class ClientController {

    @Autowired
    private ClientService clientService;

    @GetMapping("/v1/client/pop/show")
    @ApiOperation(value = "进行分页展示客户数据(可带条件)",notes = "可以传客户组Id的进行筛选，还可以进行客户姓名关键字同时筛选")
    public ReturnMsgForTable findClientList(@RequestParam(required = false,value = "groupId")@ApiParam(value = "客户组id",required = false)Integer groupId,
                                            @RequestParam(required = false,value = "searchMsg")@ApiParam(value = "搜索关键字") String searchMsg,
                                            @RequestParam(required = false,value = "now")@ApiParam(value = "当前展示到的页数") Integer now,
                                            @RequestParam(required = false,value = "size")@ApiParam(value = "一次展示多少数据")Integer size) throws InterruptedException {

        if (Objects.equals(searchMsg, "")){
            searchMsg=null;
        }

        /**
         * roc
         */
//        Thread.sleep(200);

        List<HashMap<String, Object>> clientList = clientService.findClientList(groupId, searchMsg, now, size);

        ReturnMsgForTable msg2 = new ReturnMsgForTable();
        msg2.setObjectList(clientList);
        msg2.setCurrenPage(now);
        msg2.setTotal(clientService.countClient(groupId,searchMsg));
        return msg2;

    }


    @PostMapping("/v1/client/registered")
    @ApiOperation(value = "客户注册接口",notes = "需要传一个客户类")
    public ReturnMsg clientRegistered(@RequestBody Client client){

        ReturnMsg msg = new ReturnMsg();

        Boolean aBoolean = clientService.clientRegistered(client);
        if (aBoolean){
            msg.setMsg("客户信息注册成功");
            msg.setCode(1);
        }else {
            msg.setMsg("系统好像睡着了,客户信息注册失败");
            msg.setCode(0);
        }
        return msg;
    }



    @GetMapping("/v1/get/client/data")
    public  Client getClientData(@RequestParam(required = true)Integer clientId){


        return clientService.getClientData(clientId);
    }

    @PostMapping("/v1/post/account/client")
    @ApiOperation(value = "客户界面进行收款接口")
    private ReturnMsg postAccountClient(@RequestBody(required = true) SpecialBySaleReception specialBySaleReception){

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        return clientService.postAccountClient(specialBySaleReception,Integer.valueOf(employeeIdAsString));
    }


    @PostMapping("/v1/post/invoice/client")
    @ApiOperation(value = "客户界面进行开票接口")
    private ReturnMsg postInvoiceClient(@RequestBody(required = true) SpecialInvoiceBySaleReception specialInvoiceBySaleReception){

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        return clientService.postInvoiceClient(specialInvoiceBySaleReception,Integer.valueOf(employeeIdAsString));
    }
}
